package midas.mw.crt;

import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import net.jxta.endpoint.EndpointAddress;
import net.jxta.endpoint.EndpointListener;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;

import midas.mw.cfg.CfgError;
import midas.mw.cfg.CfgLogging;
import midas.mw.cfg.log.Logger;
import midas.mw.crt.MD.PriorityTable;
import midas.mw.crt.common.IPMessage;
import midas.mw.crt.interfaces.CrtCallback;

/**
 * This class manages the sending and reception of messages using the JXTA module
 */
public class PacketForwarder implements EndpointListener
{	
	/**
	 * Instance of the CRT running in the node
	 */
	private CrtImpl crt;
	
	/**
	 * Constructor - initiate local variables
	 * @param crt Instance of the CRT running in the node
	 */
	public PacketForwarder(CrtImpl crt)
	{
		CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PacketForwarder(CrtImpl)");		
		
		this.crt = crt;
	}
	
	/**
	 * Sends the specified message 
	 * 
	 * @param ipMessage IPMessage storing the necessary information to send the message
	 */
	public void sendMessage(IPMessage ipMessage) throws CrtException
	{	
		CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PacketForwarder.sendMessage(" +  ipMessage.msgToString() + ")");		
		
		//String peerId = crt.getJxtaPlatform().seachNode(ipMessage.getDestinationNodeId());
		Iterator it = crt.getNodeIdentifierSolver().getJxtaConnectedPeers().entrySet().iterator();
		String peerId = null;
		while (it.hasNext()) 
		{
			Map.Entry entry = (Map.Entry)it.next();
			if (((String)entry.getValue()).equals(ipMessage.getDestinationNodeId()))
			{
				peerId =(String)entry.getKey();
				break;
			}														
		}	
		
    	if (peerId == null)        		
		{
			CfgError.errorReporting(CfgError.ERR_OPER, Logger.COMPONENT_CRT, CfgError.SEV_WARNING, 
					"PacketForwarder.send(IPMessage). The destination could not be resolved: " + ipMessage.msgToString(), null);
			throw new CrtException(1);
		}				
		else
    		crt.getJxtaPlatform().sendMessage(peerId, ipMessage); 			
	}
	
	/**
	 * Sends the specified message 
	 * 
	 * @param ipMessage IPMessage storing the necessary information to send the message
	 */
	public void sendMessage(IPMessage ipMessage, int offset, int length) throws CrtException
	{	
		CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PacketForwarder.send(" + ipMessage.msgToString() + ", " + offset + ", " + length + ")");
		
		//String peerId = crt.getJxtaPlatform().seachNode(ipMessage.getDestinationNodeId());
		Iterator it = crt.getNodeIdentifierSolver().getJxtaConnectedPeers().entrySet().iterator();
		String peerId = null;
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry)it.next();
			if (((String)entry.getValue()).equals(ipMessage.getDestinationNodeId()))
			{
				peerId =(String)entry.getKey();
				break;
			}														
		}	
		
    	if (peerId == null)        		
		{
    		CfgError.errorReporting(CfgError.ERR_OPER, Logger.COMPONENT_CRT, CfgError.SEV_WARNING, 
					"PacketForwarder.send(IPMessage). The destination could not be resolved", null);			
			throw new CrtException(1);
		}				
		else
    		crt.getJxtaPlatform().sendMessage(peerId, ipMessage, offset, length); 			
	}
	
	/**
	 * This method is invoked by the EndpointService for each incoming message
	 * which is addressed to this listener.
	 * 
	 * @param message - Incoming message
	 * @param srcAddress EndpointAddress of the source of the message
	 * @param dstAddress EndpointAddress of the destination of the message
	 */
	public void processIncomingMessage(Message message, EndpointAddress srcAddress, EndpointAddress dstAddress)
	{
		CfgLogging.logEvent(CfgLogging.LOG_MCALL, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PacketForwarder.processIncomingMessage(Message, EndpointAddress, EndpointAddress)");		
		
		MessageElement me1 = message.getMessageElement(Constants.MIDAS, Constants.MESSAGE_TYPE);				
		
		if (me1.toString().equals(Constants.IP_MESSAGE_BROADCAST))
		{
			String jxtaId 		= message.getMessageElement(Constants.MIDAS, Constants.JXTA_ID).toString();
			String srcNode 		= message.getMessageElement(Constants.MIDAS, Constants.SRC_NODE).toString();			
			String srcApp 		= message.getMessageElement(Constants.MIDAS, Constants.SRC_APP).toString();
			String dstApp 		= message.getMessageElement(Constants.MIDAS, Constants.DST_APP).toString();						
			MessageElement me   = message.getMessageElement(Constants.MIDAS, Constants.DATA);
			byte[] data			= me.getBytes(false);
			CfgLogging.logEvent(CfgLogging.LOG_INFO, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PacketForwarder.processIncomingMessage(Message, EndpointAddress, EndpointAddress). " + 
					" \n\tType: " + me1.toString() +  
					" \n\tJxtaId: " + jxtaId +
					" \n\tSource node: " + srcNode + 					
					" \n\tSource app: " + srcApp + 
					" \n\tDestination app : " + dstApp);					
					//" \n\tData: " + new String(data));																		
			
			//Puede que sea un mensaje dividido en partes por lo que hay que ir almacenandolas
			/*if (totalLength > data.length)
			{	
				synchronized(mID)
				{
					ipMessage = crt.getMessageReceiver().getMessage(new Long(mID));
					if (ipMessage == null)
					{	
						ipMessage = new IPMessage(srcNode, finalDst, srcApp, dstApp);
						ipMessage.setMessageContent(new byte[totalLength]);
						ipMessage.setMessageId(new Long(mID));
						ipMessage.setTotalLength(totalLength);
						ipMessage.setNumOrder(numOrder);
						System.arraycopy(data, 0, ipMessage.getMessageContent(), 0, data.length);			
						crt.getMessageReceiver().addMessagePart(ipMessage);
												
					}
					else						
						System.arraycopy(data, 0, ipMessage.getMessageContent(), Constants.MAX_MESS_SIZE*(numOrder-1), data.length);														
				
					/*ipMessage = new IPMessage(srcNode, dstNode, srcApp, dstApp, data);
					ipMessage.setMessageId(new Long(mID));
					ipMessage.setTotalLength(totalLength);
					ipMessage.setNumOrder(numOrder);
					crt.getMessageReceiver().addMessagePart(ipMessage);*/
					/*if (numOrder == numMessages)					
						//Es el ultimo						
						ipMessage = (IPMessage) crt.getMessageReceiver().getMessage(ipMessage.getMessageId());					
					else
						return;
				}
			}*/
			
			//Forward the message to the appropiate application/component			
			IPMessage ipMessage = new IPMessage(srcNode, "", srcApp, dstApp, data);
			ipMessage.setMessageType(Constants.IP_MESSAGE_BROADCAST);
			//First, we look for the destination among the registered components (but not the MW)
			CrtCallback crtUser = null;
			crtUser = (CrtCallback) crt.getCRTUser(dstApp);
			if (crtUser != null)
				crtUser.processIncomingMessage(ipMessage);	
			else
			{
				//We look for the destination between components
				crtUser = (CrtCallback) crt.getMiddlewareInterface().getRunningComponent(dstApp);
				if (crtUser != null)
					crtUser.processIncomingMessage(ipMessage);
				//We look for the destination between MIDAS applications				
				else
					CfgError.errorReporting(CfgError.ERR_INFO, Logger.COMPONENT_CRT, CfgError.SEV_INFORMAL, "PacketForwarder.processIncomingMessage(Message, EndpointAddress, EndpointAddress). " + 
							"The message destination user is not registered", null);			
			}
			
			//If it is a Central Node, forward the message
			if (crt.getJxtaPlatform().getCentralNodeId() == null)
			{				
				try {
					crt.getJxtaPlatform().propagateMessage(message);					
					//Se lo manda tb. a los nodos JXME que tiene conectados
					ipMessage.setMessageType(Constants.IP_MESSAGE_BROADCAST);
					Vector jxmeNodes = crt.getNodeIdentifierSolver().getOwnJXMENodes();							
					for (int i=0; i<jxmeNodes.size(); i++)
					{						
						ipMessage.setDestinationNodeId((String)jxmeNodes.get(i));
						crt.getJxtaPlatform().forwardMessage(ipMessage);
					}
				} catch (CrtException crte){
					crte.printStackTrace();
				}										
			}				
		}
		
		//Puede ser el destino final o bien recibirlo en el proxy para reenviarlo
		else if (me1.toString().equals(Constants.IP_MESSAGE_REQUEST))
		{			
			String jxtaId 		= message.getMessageElement(Constants.MIDAS, Constants.JXTA_ID).toString();				
			String srcNode 		= message.getMessageElement(Constants.MIDAS, Constants.SRC_NODE).toString();			
			String dstNode 		= message.getMessageElement(Constants.MIDAS, Constants.DST_NODE).toString();			
			String srcApp 		= message.getMessageElement(Constants.MIDAS, Constants.SRC_APP).toString();			
			String dstApp 		= message.getMessageElement(Constants.MIDAS, Constants.DST_APP).toString();			
			//String data 		= message.getMessageElement(Constants.MIDAS, Constants.DATA).toString();
			Long mID    		= new Long(message.getMessageElement(Constants.MIDAS,Constants.MESSAGE_ID).toString());
			
			
			int totalLength	= Integer.parseInt(message.getMessageElement(Constants.MIDAS,Constants.TOTAL_LENGTH).toString());			
			int numMessages	= Integer.parseInt(message.getMessageElement(Constants.MIDAS,Constants.NUM_MESSAGES).toString());
			int numOrder	= Integer.parseInt(message.getMessageElement(Constants.MIDAS,Constants.NUM_ORDER).toString());					
			
			String confirmation	= message.getMessageElement(Constants.MIDAS,Constants.CONFIRMATION).toString();			
			MessageElement me 	= message.getMessageElement(Constants.MIDAS,Constants.FINAL_DST_NODE);
			String finalDst 	= null;
			if (me != null)
				finalDst = message.getMessageElement(Constants.MIDAS,Constants.FINAL_DST_NODE).toString();
		
			me   = message.getMessageElement(Constants.MIDAS, Constants.DATA);
			byte[] data			= me.getBytes(false);			
			//unconvert(data);
			CfgLogging.logEvent(CfgLogging.LOG_INFO, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PacketForwarder.processIncomingMessage(Message, EndpointAddress, EndpointAddress). " +			
					" \n\tType: " + me1.toString() +
					" \n\tJxtaId: " + jxtaId +
					" \n\tSource node: " + srcNode + 
					" \n\tDestination node: " + dstNode +
					" \n\tSource app: " + srcApp + 
					" \n\tDestination app : " + dstApp +
					" \n\tMessage Id : " + mID +
					" \n\tTotal length: " + totalLength +
					" \n\tNum messages : " + numMessages +
					" \n\tNum order : " + numOrder +					
					" \n\tConfirmation : " + confirmation +
					" \n\tFinalDest : " + finalDst);// + 
					//" \n\tData: " + new String(data));
			
			//La condicion (finalDst != null) se puede dar: 
			//    - en el CN cuando un node JXTA esta mandando algo a un nodo JXME
			//    - en un nodo JXTA que recibe un mensaje redirigido del CN de un nodo JXME
			//Por eso para tratar este caso incluimos la segunda condicion
			IPMessage ipMessage = null;
			if (finalDst != null && !finalDst.equals(crt.getOwnNodeId()))
			{
				boolean bConfirmation = false;
				if (confirmation.equals("1"))
					bConfirmation = true;
				ipMessage = new IPMessage(srcNode, finalDst, srcApp, dstApp, data);
				//Si viene de un nodo JXTA mandando un mensaje a un nodo JXME, en el CN
				//debemos darle un rango mayor
				//long myMessageId = Long.valueOf(mID).longValue() + 2000;
				//ipMessage.setMessageId(new Long(myMessageId));
				ipMessage.setMessageId(mID);
				ipMessage.setTotalLength(totalLength);
				ipMessage.setNumMessages(numMessages);
				ipMessage.setNumOrder(numOrder);
				ipMessage.setConfirmation(bConfirmation);
				//Lo intentamos enviar a un nodo JXME
				try {
					crt.getJxtaPlatform().forwardMessage(ipMessage);
					CfgLogging.logEvent(CfgLogging.LOG_INFO, Logger.COMPONENT_CRT, CfgLogging.SEV_INFORMAL, "PacketForwarder.processIncomingMessage(Message, EndpointAddress, EndpointAddress). Forwarding the message");
				} catch (CrtException crte){
					crte.printStackTrace();
				}
			}
			//Si finalDst es null o el destino final es el propio nodo o ambas,
			//que este nodo es el destino
			else
			{					
				if (confirmation.equals("1"))
				{					
					ipMessage = new IPMessage(dstNode ,srcNode, dstApp, srcApp, "OK".getBytes());
					ipMessage.setMessageId(mID);
					ipMessage.setMessageType(Constants.IP_MESSAGE_RESPONSE);
					if (crt.getNodeIdentifierSolver().isJXMENode(srcNode))
					{
						ipMessage.setFinalDestinationNodeId(srcNode);
						try {
								crt.getJxtaPlatform().sendMessage(jxtaId, ipMessage);
						} catch (CrtException crt){
							crt.printStackTrace();
						}
					}
					else
					{
						try {
							crt.getJxtaPlatform().forwardMessage(ipMessage);
						} catch (CrtException crt){
							crt.printStackTrace();
						}
					}
					ipMessage = null;
				}
						
				//Puede que sea un mensaje dividido en partes por lo que hay que ir almacenandolas
				if (totalLength > data.length)
				{	
					synchronized(srcNode)
					{
						ipMessage = crt.getMessageReceiver().getMessage(srcNode, mID);
						if (ipMessage == null)
						{											
							ipMessage = new IPMessage(srcNode, dstNode, srcApp, dstApp, new byte[totalLength]);					
							
							ipMessage.setMessageId(mID);
							ipMessage.setTotalLength(totalLength);
							ipMessage.setNumOrder(numOrder);
							System.arraycopy(data, 0, ipMessage.getMessageContent(), 0, data.length);			
							crt.getMessageReceiver().addMessage(ipMessage);													
						}
						else
							System.arraycopy(data, 0, ipMessage.getMessageContent(), Constants.MAX_MESS_SIZE*(numOrder-1), data.length);						
					
						/*ipMessage = new IPMessage(srcNode, dstNode, srcApp, dstApp, data);
						ipMessage.setMessageId(new Long(mID));
						ipMessage.setTotalLength(totalLength);
						ipMessage.setNumOrder(numOrder);
						crt.getMessageReceiver().addMessagePart(ipMessage);*/
						if (numOrder == numMessages)					
						{
							//Es el ultimo						
							ipMessage = (IPMessage) crt.getMessageReceiver().getMessage(srcNode, ipMessage.getMessageId());					
							crt.getMessageReceiver().removeMessage(srcNode, ipMessage.getMessageId());
							//crt.getMessageReceiver().printTable();
						}
						else
							return;
					}
				}
				//Forward the message to the appropiate application/component				
				if (ipMessage == null)
				{
					ipMessage = new IPMessage(srcNode, dstNode, srcApp, dstApp, data);					
					ipMessage.setMessageId(mID);
				}
				
				if (ipMessage.getMessageContent().length != totalLength)				
				{
					//System.out.println("NO TENGO EL MENSAJE COMPLETO");
				}
				else
				{
					CfgLogging.logEvent(CfgLogging.LOG_INFO, Logger.COMPONENT_CRT, CfgLogging.SEV_INFORMAL, "PacketForwarder.processIncomingMessage(Message, EndpointAddress, EndpointAddress). Sending the message to the application");
					//First, we look for the destination among the registered components (but not the MW)
					CrtCallback crtUser = null;
					crtUser = (CrtCallback) crt.getCRTUser(dstApp);
					if (crtUser != null)
						crtUser.processIncomingMessage(ipMessage);	
					else
					{
						//We look for the destination between components
						try {
	        				//We look for the destination between components
							crtUser = (CrtCallback) crt.getMiddlewareInterface().getRunningComponent(dstApp);
        				} catch (ClassCastException cce){}
						if (crtUser != null)
							crtUser.processIncomingMessage(ipMessage);
						//We look for the destination between MIDAS applications						
						else
							CfgError.errorReporting(CfgError.ERR_INFO, Logger.COMPONENT_CRT, CfgError.SEV_INFORMAL, "PacketForwarder.processIncomingMessage(Message, EndpointAddress, EndpointAddress). " + 
									"The message destination user is not registered", null);						
					}				
				}
			}
		}
		
		//Puede ser el destino final o bien recibirlo en el proxy para reenviarlo
		else if (me1.toString().equals(Constants.IP_MESSAGE_RESPONSE))
		{
			String jxtaId 		= message.getMessageElement(Constants.MIDAS, Constants.JXTA_ID).toString();
			String srcNode 		= message.getMessageElement(Constants.MIDAS, Constants.SRC_NODE).toString();
			String dstNode 		= message.getMessageElement(Constants.MIDAS, Constants.DST_NODE).toString();
			String srcApp 		= message.getMessageElement(Constants.MIDAS, Constants.SRC_APP).toString();
			String dstApp 		= message.getMessageElement(Constants.MIDAS, Constants.DST_APP).toString();
			//String data 		= message.getMessageElement(Constants.MIDAS, Constants.DATA).toString();
			Long mID    		= new Long(message.getMessageElement(Constants.MIDAS,Constants.MESSAGE_ID).toString());
			MessageElement me 	= message.getMessageElement(Constants.MIDAS,Constants.FINAL_DST_NODE);
			String finalDst 	= null;
			if (me != null)
				finalDst = message.getMessageElement(Constants.MIDAS,Constants.FINAL_DST_NODE).toString();
			
			me   = message.getMessageElement(Constants.MIDAS, Constants.DATA);
			byte[] data			= me.getBytes(false);
			
			CfgLogging.logEvent(CfgLogging.LOG_INFO, Logger.COMPONENT_CRT, CfgLogging.SEV_DEBUG, "PacketForwarder.processIncomingMessage(Message, EndpointAddress, EndpointAddress). " + 
					" \n\tType: " + me1.toString() +
					" \n\tJxtaId: " + jxtaId +
					" \n\tSource node: " + srcNode + 
					" \n\tDestination node: " + dstNode +
					" \n\tSource app: " + srcApp + 
					" \n\tDestination app : " + dstApp +
					" \n\tMessage Id : " + mID +
					" \n\tFinalDest : " + finalDst); 
					//" \n\tData: " + new String(data));
			
			//La condicion (finalDst != null) se puede dar: 
			//    - en el CN cuando un node JXTA esta mandando algo a un nodo JXME
			//    - en un nodo JXTA que recibe un mensaje redirigido del CN de un nodo JXME
			//Por eso para tratar este caso incluimos la segunda condicion
			if (finalDst != null && !finalDst.equals(crt.getOwnNodeId()))
			{				
				IPMessage ipMessage = new IPMessage(srcNode, finalDst, srcApp, dstApp, "OK".getBytes());
				//Disminuimos en 1000 porque antes lo habiamos aumentado
				//long realMessageId = Long.valueOf(mID).longValue() - 1000;
				//ipMessage.setMessageId(new Long(realMessageId));
				ipMessage.setMessageId(mID);
				ipMessage.setMessageType(Constants.IP_MESSAGE_RESPONSE);								
				//Lo intentamos enviar a un nodo JXME
				try {
					crt.getJxtaPlatform().forwardMessage(ipMessage);
				} catch (CrtException crte){
					crte.printStackTrace();
				}
			}
			//Si finalDst es null o el destino final es el propio nodo o ambas,
			//significa que este nodo es el destino
			else
			{						
				IPMessage receivedMessage = (IPMessage)crt.getMessageDispatcher().getMessageList().get(mID);
				if (receivedMessage != null)
				{
					/*System.out.println("MEMORIA USADA ANTES DE RECIBIR CONFIRMACION: " + (Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()));
					System.out.println("MEMORIA LIBRE ANTES DE RECIBIR CONFIRMACION: " + Runtime.getRuntime().freeMemory());*/
					
					/*crt.getMessageDispatcher().getTimeQueue().deleteMessage(receivedMessage);
					crt.getMessageDispatcher().getNodeList().deleteMessage(receivedMessage);
					crt.getMessageDispatcher().getMessageList().remove(mID);					
					*/
					
					/*System.gc();					
					System.out.println("MEMORIA USADA DESPUES DE RECIBIR CONFIRMACION: " + (Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()));
					System.out.println("MEMORIA LIBRE DESPUES DE RECIBIR CONFIRMACION: " + Runtime.getRuntime().freeMemory());*/
				
					/*WaitingThread wt = (WaitingThread)crt.getMessageDispatcher().getWaitingThreads().get(mID);
					if (wt != null)
					{						
						wt.bResolved = true;
						/*synchronized(wt.responseLock) {
							wt.responseLock.notify();
						}
						
						crt.getMessageDispatcher().getWaitingThreads().remove(mID);*/
						/*wt.stopThread();
						
					}
					else
						crt.getMessageDispatcher().getNodeList().setStatus(srcNode, false);
					*/
				}
				//Forward the message to the appropiate application/component
				IPMessage ipMessage = new IPMessage(srcNode, dstNode, srcApp, dstApp, data);
				ipMessage.setMessageId(mID);
				//First, we look for the destination among the registered components (but not the MW)
				CrtCallback crtUser = null;
				crtUser = (CrtCallback) crt.getCRTUser(dstApp);
				if (crtUser != null)
						crtUser.processDeliveredMessage(ipMessage);	
				else
				{
					//We look for the destination between components
					crtUser = (CrtCallback) crt.getMiddlewareInterface().getRunningComponent(dstApp);
					if (crtUser != null)
							crtUser.processDeliveredMessage(ipMessage);
					//We look for the destination between MIDAS applications					
					else
						CfgError.errorReporting(CfgError.ERR_INFO, Logger.COMPONENT_CRT, CfgError.SEV_INFORMAL, "PacketForwarder.processIncomingMessage(Message, EndpointAddress, EndpointAddress). " + 
								"The message destination user is not registered", null);								
				}	
				try {
					Object obj = crt.getMessageDispatcher().getNodeList().getNodeQueue(srcNode);
					if (obj instanceof PriorityTable)
						((PriorityTable)obj).getQueueThread().responseGot();		
				} catch (Exception e){e.printStackTrace();}
				
			}
			
			//crt.getMessageDispatcher().processNodeAvailability(srcNode);
			
			/*if (!crt.getMessageDispatcher().getNodeList().isEmptyNodeQueue(srcNode))
			{
				crt.getMessageDispatcher().getNodeList().setStatus(srcNode, false);
				crt.getMessageDispatcher().processNodeAvailability(srcNode);
			}*/
		}
		
		/*else if (me1.toString().equals(Constants.ACTION_ADD))
		{
			String jxtaId 		= null;
			String nodeId 		= null;
			String ipAddress 	= null;
			message.removeMessageElement(me1);
			for (Iterator en = message.getMessageElementsOfNamespace(Constants.MIDAS); en.hasNext();)
			{
				jxtaId 		= ((MessageElement)en.next()).toString();
				nodeId 		= ((MessageElement)en.next()).toString();				
				ipAddress 	= ((MessageElement)en.next()).toString();
				
				if ((String)crt.getNodeIdentifierSolver().getNodeId(jxtaId) == null)						
					crt.getNodeIdentifierSolver().put(jxtaId, nodeId, ipAddress);
									
				CfgLogging.logEvent(CfgLogging.LOG_INFO, "CRT.PacketForwarder", 0, "processIncomingMessage(Message, EndpointAddress, EndpointAddress). " + 
						" \n\tType: " + me1.toString() +  
						" \n\tJXTA identifier: " + jxtaId + 
						" \n\tMIDAS node identifier: " + nodeId +
						" \n\tIP Addresses: " + ipAddress);
				try {
					crt.getNetworkManager().notifyPresence(nodeId, true);
				} catch (CrtException crte) {					
				}
			} 			
		}
		else if (me1.toString().equals(Constants.ACTION_DELETE))
		{
			String jxtaId 	= message.getMessageElement(Constants.MIDAS, Constants.JXTA_ID).toString();	
			String nodeId 	= message.getMessageElement(Constants.MIDAS, Constants.MIDAS_ID).toString();	
			crt.getNodeIdentifierSolver().remove(jxtaId); 
			CfgLogging.logEvent(CfgLogging.LOG_INFO, "CRT.PacketForwarder", 0, "processIncomingMessage(Message, EndpointAddress, EndpointAddress). " + 
					" \n\tType: " + me1.toString() +  
					" \n\tJXTA identifier: " + jxtaId + 
					" \n\tMIDAS node identifier: " + nodeId);
			try {
				if ((String)crt.getNodeIdentifierSolver().getNodeId(jxtaId) == null)						
					crt.getNodeIdentifierSolver().remove(nodeId);
				crt.getNetworkManager().notifyPresence(nodeId, false);
			} catch (CrtException crte) {				
			}
		}*/	
		else
			CfgError.errorReporting(CfgError.ERR_INFO, Logger.COMPONENT_CRT, CfgError.SEV_WARNING, "PacketForwarder.processIncomingMessage(Message, EndpointAddress, EndpointAddress). " + 
					"Undefined type of message", null);			
		
	}		
}
